package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.exception.*;
import com.sky.mapper.admin.EmployeeMapper;
import com.sky.pojo.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();//

        //1、根据用户名查询数据库中的数据  select * from employee where username = #{username}
        Employee employee = employeeMapper.getByUsername(username); //

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    /**
     * 添加员工
     *
     * @param dto
     * @return
     */
    @Override
    public Result<String> add(EmployeeDTO dto) {
        //查看用户名是否重复
        String dbUsername = employeeMapper.findByUsername(dto.getUsername());
        if (dbUsername != null) {
            throw new EmployeeNameExistException(dto.getUsername() + "  该用户名已存在，请重新输入！");
        }
        //创建一个员工对象
        Employee employee = new Employee();
        //将前端的数据放到对象中
        BeanUtils.copyProperties(dto, employee);
        //补全剩余的数据
        employee.setStatus(1);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());
        //调用Mapper层，保存数据
        employeeMapper.add(employee);

        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param empPageDto
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO empPageDto) {
        //开启分页查询
        PageHelper.startPage(empPageDto.getPage(), empPageDto.getPageSize());
        //调用Mapper层,获取所有对象数据
        List<Employee> employees = employeeMapper.pageQuery(empPageDto.getName());
        //将类型转换成 Page<Employee> 类型
        Page<Employee> pageEmployees = (Page<Employee>) employees;
        //封装成PageResult
        PageResult result = new PageResult(pageEmployees.getTotal(), pageEmployees.getResult());

        return result;
    }

    /**
     * 是否启用员工账号
     *
     * @param status 状态码
     * @param id     员工id
     * @return void
     */
    @Override
    public void isEnable(Integer status, Long id) {
        employeeMapper.isEnable(status, id);
    }

    /**
     * 根据id查询回显员工数据
     *
     * @param id
     * @return
     */
    @Override
    public Result<Employee> findById(Integer id) {
        return Result.success(employeeMapper.findById(id));
    }

    /** 修改员工信息
     * @param employee 修改的员工信息
     */
    @Override
    public void update(Employee employee) {
        //1.补全数据
//        employee.setUpdateUser(BaseContext.getCurrentId());//修改人的id为当前修改人的id
//        employee.setUpdateTime(LocalDateTime.now());
        //2.调用Mapper层,存储数据
        employeeMapper.update(employee);
    }


}
